/**
 * DataSourceAspect.java 
 * Copyright © 2015-2016, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年4月26日
 */
package com.pshare.framework.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import com.pshare.framework.annotation.DataSource;
import com.pshare.framework.db.DynamicDataSourceHolder;

/**
 * 数据源管理切面
 */
public class DataSourceAspect {

	public void before(JoinPoint point) {
		Object target = point.getTarget();
		String method = point.getSignature().getName();

		Class<?>[] classz = target.getClass().getInterfaces();
		Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
		
		try {
			Method m = classz[0].getMethod(method, parameterTypes);
			if (m != null && m.isAnnotationPresent(DataSource.class)) {
				DataSource data = m.getAnnotation(DataSource.class);
				DynamicDataSourceHolder.putDataSource(data.value());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
